package com.freeman.cement.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.freeman.cement.entity.Dept;
import com.freeman.cement.entity.ResponseResult;
import com.freeman.cement.exception.AppException;
import com.freeman.cement.exception.AppExceptionCodeMsg;
import com.freeman.cement.mapper.EventTypeDao;
import com.freeman.cement.entity.EventType;
import com.freeman.cement.service.EventTypeService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service
public class EventTypeServiceImpl implements EventTypeService {
    @Resource
    private EventTypeDao eventTypeDao;

    @Override
    public ResponseResult selectAllEventType(int pageNo, int pageSize) {
        //验证分页范围
        if (pageNo < 1 || pageSize > 1000) {
            throw new AppException(AppExceptionCodeMsg.PAGE_NO_OR_SIZE_WRONG);
        }
        PageHelper.startPage(pageNo, pageSize, true);
        List<EventType> list = eventTypeDao.selectAllEventType();
        PageInfo<EventType> pageInfo = new PageInfo<EventType>(list);
        return ResponseResult.success(pageInfo);
    }

    @Override
    public ResponseResult selectSubEventType(int dutyroomId, int pageNo, int pageSize) {
        //验证分页范围
        if (pageNo < 1 || pageSize > 1000) {
            throw new AppException(AppExceptionCodeMsg.PAGE_NO_OR_SIZE_WRONG);
        }

        PageHelper.startPage(pageNo, pageSize, true);
        List<EventType> list = eventTypeDao.selectSubEventType(dutyroomId);
        PageInfo<EventType> pageInfo = new PageInfo<EventType>(list);
        return ResponseResult.success(pageInfo);
    }

    @Override
    public ResponseResult selectEventTypeByKeyword(String keyword, int pageNo, int pageSize) {
        if (pageNo < 1 || pageSize > 1000) {
            throw new AppException(AppExceptionCodeMsg.PAGE_NO_OR_SIZE_WRONG);
        }
        PageHelper.startPage(pageNo, pageSize, true);
        QueryWrapper<EventType> qw = new QueryWrapper<>();
        qw.like("eventName", keyword).or().like("eventType", keyword).orderByDesc("id");
        List<EventType> list = eventTypeDao.selectList(qw);
        PageInfo<EventType> pageInfo = new PageInfo<EventType>(list);
        return ResponseResult.success(pageInfo);
    }

    @Override
    public ResponseResult addEventType(EventType eventType) {
        if(eventType.getEventName() != null && StringUtils.isNotBlank(eventType.getEventName())
        && eventType.getEventType() != null && StringUtils.isNotBlank(eventType.getEventType())) {
            List<EventType> list = eventTypeDao.selectEventTypeByNameOrType(eventType);
            if (list.size() > 0) {
                throw new AppException(AppExceptionCodeMsg.EVENT_TYPE_NAME_OR_CODE_IS_SAME);
            } else {
                if (eventTypeDao.insert(eventType) > 0) {
                    return ResponseResult.success("添加成功", eventType.getId());
                } else {
                    throw new AppException(AppExceptionCodeMsg.ADD_EVENT_TYPE_ERROR);
                }
            }
        } else {
            throw new AppException(AppExceptionCodeMsg.CONTENT_IS_NULL);
        }
    }

    @Override
    public ResponseResult deleteEventType(int id) {
        if (id > 0) {
            EventType eventType = eventTypeDao.selectById(id);
            if (eventType != null) {
                if (eventTypeDao.deleteById(id) > 0) {
                    return ResponseResult.success("删除成功", null);
                } else {
                    throw new AppException(AppExceptionCodeMsg.DELETE_EVENT_TYPE_ERROR);
                }
            } else {
                throw new AppException(AppExceptionCodeMsg.ID_NOT_EXISTS);
            }
        } else {
            throw new AppException(AppExceptionCodeMsg.ID_IS_NULL);
        }
    }

    private boolean checekEventName(String name) {
        QueryWrapper<EventType> qw = new QueryWrapper<>();
        qw.select("eventName").eq("eventName", name);
        Long a = eventTypeDao.selectCount(qw);
        if (a > 0) {
            throw new AppException(AppExceptionCodeMsg.EVENT_NAME_EXIST);
        }
        return true;
    }

    private boolean checekEventType(String type) {
        QueryWrapper<EventType> qw = new QueryWrapper<>();
        qw.select("eventType").eq("eventType", type);
        Long a = eventTypeDao.selectCount(qw);
        if (a > 0) {
            throw new AppException(AppExceptionCodeMsg.EVENT_TYPE_EXIST);
        }
        return true;
    }

    @Override
    public ResponseResult updateEventType(EventType eventType) {
        if (eventType != null && eventType.getId() != null && eventType.getId() > 0) {
            EventType old = eventTypeDao.selectById(eventType.getId());
            if (old != null) {
                if (eventType.getEventName() != null
                        && StringUtils.isNotBlank(eventType.getEventName())
                        && !eventType.getEventName().equals(old.getEventName())) {
                    checekEventName(eventType.getEventName());
                }
                if (eventType.getEventType() != null
                        && StringUtils.isNotBlank(eventType.getEventType())
                        && !eventType.getEventType().equals(old.getEventType())) {
                    checekEventType(eventType.getEventType());
                }
                eventType.setId(old.getId());
                if (eventTypeDao.updateById(eventType) > 0) {
                    return ResponseResult.success("修改成功", null);
                } else {
                    throw new AppException(AppExceptionCodeMsg.UPDATE_EVENT_TYPE_ERROR);
                }
            } else {
                throw new AppException(AppExceptionCodeMsg.ID_NOT_EXISTS);
            }
        } else {
            throw new AppException(AppExceptionCodeMsg.ID_IS_NULL);
        }
    }

}
